package jmu.cultural_tourism.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jmu.cultural_tourism.entity.Route;
import jmu.cultural_tourism.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 路径表(Route)表控制层
 *
 * @author makejava
 * @since 2025-07-20 20:46:22
 */
@RestController
@Tag(name = "路径信息接口")
@RequestMapping("route")
public class RouteController {
    /**
     * 服务对象
     */
    @Autowired
    private RouteService routeService;

    /**
     * 新增路线
     */
    @Operation(summary = "新增路线")
    @PostMapping("add")
    public ResponseEntity<Route> add(@RequestBody Route route) {
        return ResponseEntity.ok(this.routeService.insert(route));
    }

    /**
     * 删除路线
     */
    @Operation(summary = "删除路线")
    @DeleteMapping("del/{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.routeService.deleteById(id));
    }

    /**
     * 编辑路线信息
     */
    @Operation(summary = "编辑路线信息")
    @PutMapping("edit")
    public ResponseEntity<Route> edit(@RequestBody Route route) {
        return ResponseEntity.ok(this.routeService.update(route));
    }

    /**
     * 根据路线ID查找路线
     */
    @Operation(summary = "根据路线ID查找路线")
    @GetMapping("findById/{id}")
    public ResponseEntity<Route> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.routeService.queryById(id));
    }

    /**
     * 分页条件查询
     */
    @PostMapping({"page/{pageNo}/{pageSize}", "page/{pageNo}", "page"})
    public ResponseEntity<Page<Route>> queryByPage(
            @RequestBody(required = false) Route route,
            @PathVariable(value = "pageNo", required = false) Integer pageNo,
            @PathVariable(value = "pageSize", required = false) Integer pageSize) {
        if (pageNo == null) {
            pageNo = 0;
        } else {
            pageNo = pageNo - 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        return ResponseEntity.ok(this.routeService.queryByPage(route, pageRequest));
    }

    /**
     * 显示所有路径信息
     */
    @Operation(summary = "显示所有路径信息")
    @GetMapping("all")
    public ResponseEntity<List<Route>> getAllRoutes() {
        return ResponseEntity.ok(this.routeService.getAllRoutes());
    }
}